package com.example.a28796.beautiful.styfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a28796.beautiful.R;
import com.example.a28796.beautiful.styAdapter.HotAdapter;
import com.example.a28796.beautiful.styAdapter.ReHotAdapter;
import com.example.a28796.beautiful.stybean.HotBean;
import com.example.a28796.beautiful.stynet.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/8/30.
 */
public class FragmentNice extends Fragment {
    private List<HotBean.ObjBean.ListDataBean> mData;
    private int currentPage = 1;

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private LinearLayoutManager layoutManager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.hot_fragment,null);
        initView(view);
        initData();
        setData();
        return view;
    }


    private void setData() {
        layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                loadMore(1);
            }
        });
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState==RecyclerView.SCROLL_STATE_IDLE){
                    int last = layoutManager.findLastVisibleItemPosition();
                    if(last==mData.size()-1){
                        loadMore(currentPage);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

    }

    private void initData() {
        loadMore(1);
        mData = new ArrayList<>();
        mAdapter = new ReHotAdapter(mData,getActivity());
    }

    private void loadMore(final int position){
        String path = String.format(NetConfig.SHEQU_NICE,currentPage);
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                HotBean hotBean = new Gson().fromJson(result, HotBean.class);

                currentPage++;
                if(position==1){
                    mData.clear();
                }
                mData.addAll(hotBean.getObj().getListData());
                mAdapter.notifyDataSetChanged();
                mAdapter.notifyDataSetChanged();
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络连接失败，请检查网络", Toast.LENGTH_SHORT).show();
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_hot_show);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.sl_main_test);
    }
}
